package com.example.teamtribe.main.chat.request;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.Application;
import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.common.send.SendActivity;
import com.example.teamtribe.database.Request;
import com.example.teamtribe.database.Team;
import com.example.teamtribe.database.TeamRequest;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.Utility;

import butterknife.BindView;

public class RequestActivity extends BaseActivity implements View.OnClickListener, RequestContract.View {

    public static final int RESULT_OK = -2;

    @BindView(R.id.iv_head) ImageView ivHead;
    @BindView(R.id.tv_nickname) TextView tvNickname;
    @BindView(R.id.tv_info) TextView tvInfo;
    @BindView(R.id.tv_reason) TextView tvReason;
    @BindView(R.id.tv_response) TextView tvResponse;

    private RequestContract.Presenter presenter;
    private Request request;
    private User user;

    private TeamRequest teamRequest;
    private Team team;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request);
        TextView tvTitle = findViewById(R.id.tv_title);
        Object object = getIntent().getParcelableExtra("request");

        if (object instanceof Request) {
            request = (Request) object;
            tvTitle.setText("好友请求");
            user = Utility.getT(request.getUserId(), User.class);
            presenter = new RequestPresenter(this);
            findViewById(R.id.bt_disagree).setOnClickListener(this);
            findViewById(R.id.bt_agree).setOnClickListener(this);
            setUserInfo();
            setResultView(request.getResult());
        } else {
            teamRequest = (TeamRequest) object;
            tvTitle.setText("社团请求");
            team = Utility.getT(teamRequest.getTeamId(), Team.class);
            setResultView(teamRequest.getResult());
            setTeamInfo();
            setResultView(teamRequest.getResult());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_disagree:
                Intent intent = new Intent(RequestActivity.this, SendActivity.class);
                intent.putExtra("request", request);
                intent.putExtra("user", user);
                startActivityForResult(intent, SendActivity.REQUEST_RESP);
                break;

            case R.id.bt_agree:
                presenter.agree(request.getRealId());
                break;

            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SendActivity.REQUEST_RESP && resultCode == SendActivity.RESULT_OK) {
            request = data.getParcelableExtra("request");
            setUserInfo();
            setResultView(request.getResult());
        }
    }

    @Override
    public void next() {
        Application.getBinder().getFriends();//同意或拒绝后刷新好友列表和消息内容
        Application.getBinder().getResponseMeg();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                request.setResult(1);
                request.saveOrUpdate();
                finish();
            }
        });
    }

    private void setUserInfo() {
        Glide.with(this).load(Utility.getUri(user.getPortrait())).into(ivHead);
        tvNickname.setText(user.getNickname());
        tvInfo.setText(user.getGenderString());
        tvReason.setText(request.getRequestReason());
        tvResponse.setText(request.getResponseReason());
    }

    private void setTeamInfo() {
        Glide.with(this).load(Utility.getUri(team.getPortrait())).into(ivHead);
        tvNickname.setText(team.getName());
        tvInfo.setText(team.getComment());
    }

    private void setResultView(int result) {
        TextView tvResult = findViewById(R.id.tv_result);
        LinearLayout llResponse = findViewById(R.id.ll_response);
        tvResult.setVisibility(View.VISIBLE);
        llResponse.setVisibility(View.GONE);
        if (result == 1) {
            tvResult.setText("已同意");
        } else if (result == 2) {
            tvResult.setText("已拒绝");
        } else if (result == user.getRealId()) {
            tvResult.setText("等待回复");
        } else {
            tvResult.setVisibility(View.GONE);
            llResponse.setVisibility(View.VISIBLE);
        }
    }
}
